package com.prs.crm.action.cqunicom;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cqunicom.Ticketer;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.exception.InValidDeleteException;
import com.prs.crm.service.cqunicom.TicketerService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.sys.LogService;

/**
 * @author  WangTing
 *
 * 创建时间：2011-3-7
 * 
 */

public class TicketerAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Ticketer ticketer;
	private TicketerService ticketerService;
	private Integer[] ticketerIds;
	private String affirmPwd;
	
	private PaginationSupport pagination;
	private LogService logService;
	
	private Person person;
	private PersonService personService;
	
	@Action("editTicketer")
	public String edit() {
		return SUCCESS;
	}
	
	@Action("viewTicketer")
	public String view() {
		return SUCCESS;
	}
	
	@Action("listTicketer")
	public String listTicketer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Ticketer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTicketerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("alterTicketer")
	public String alterTicketer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Ticketer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTicketerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveTicketer", results = {
			@Result(name = "success", type = "redirect", location = "editTicketer"),
			@Result(name = "none", type = "redirect", location = "listTicketer"),
			@Result(name = "error", type = "chain", location = "editTicketer") })
	public String save() {
		if (ticketer.getId() == null) {
			  List<Person> per=this.getPersonService().getAllPersons();
		        int diacInit=0;
		        for(Person p:per){
		        	if(p.getLoginCode().equals(ticketer.getName1())){
		        		diacInit++;
		        	}
		        }
		        if(diacInit>0){
		        	this.setPrompt("登录用户名重复");
		        	return "error";
		        }else{
		   this.getTicketerService().save(ticketer);
		   //票务人员账号注册信息
		    Person  persons = new Person();
			persons.setLoginCode(ticketer.getName1());
			persons.setLoginPwd(ticketer.getPassword());
			persons.setRole(person.getRole());
			persons.setUserStyle(ticketer.getUserStyle());
			persons.setUserNumber(person.getUserNumber());
//			persons.setTicketer(ticketer);
//			persons.setMovie(ticketer.getMovieTick());
			persons.setUserName("票务人员");
			persons.setMarkName("否");
			persons.setUserTel(ticketer.getPhone_No());
			this.getPersonService().save(persons);
			this.setPrompt("设置票务人员成功");
			return SUCCESS;
		        }
		   
		}else{
			/* List<Person> per=this.getPersonService().getAllPersons();
		        int diacInit=0;
		        for(Person p:per){
		        	if(p.getLoginCode().equals(ticketer.getName1())){
		        		diacInit++;
		        	}
		        }
		        if(diacInit>0){
		        	this.setPrompt("票务人员名字重复");
		        	return "error";
		        }else{*/
			Person persons=this.getPersonService().getPersonByTicketer(ticketer.getId(), ticketer.getUserStyle());
	
			
			persons.setLoginCode(ticketer.getName1());
			persons.setLoginPwd(ticketer.getPassword());
			
			this.getPersonService().saveOrUpdate(persons);
			
			
			this.getTicketerService().save(ticketer);
			this.setPrompt("设置票务人员成功");
			   return SUCCESS;
			
		       // }
		}
		   
		  
	}

	@Action(value = "removeTicketer", results = {
			@Result(name = "success", type = "redirect", location = "listTicketer"),
			@Result(name = "error", type = "chain", location = "listTicketer") })
	public String remove() {
		try {
			if (ticketerIds == null) {
				this.addActionError("请选择要删除的送票员");
				return ERROR;
			}
			this.getTicketerService().batchRemove(ticketerIds);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			return ERROR;
		}
	}
	
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public Ticketer getTicketer() {
		return ticketer;
	}

	public void setTicketer(Ticketer ticketer) {
		this.ticketer = ticketer;
	}

	public TicketerService getTicketerService() {
		return ticketerService;
	}

	public void setTicketerService(TicketerService ticketerService) {
		this.ticketerService = ticketerService;
	}

	public Integer[] getTicketerIds() {
		return ticketerIds;
	}

	public void setTicketerIds(Integer[] ticketerIds) {
		this.ticketerIds = ticketerIds;
	}

	public String getAffirmPwd() {
		return affirmPwd;
	}

	public void setAffirmPwd(String affirmPwd) {
		this.affirmPwd = affirmPwd;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	
	

}
